const baseURL='/apa';
const headers={
    'Accept': 'application/json',
    'Content-Type': 'application/json'
}
function json2url(data){
    //{a：1,b:2}  =》 a=1&b=2
    let arr=[];
    for(let name in data){
        arr.push(name+'='+data[name])
    }
    //[a=1,b=2]
    return arr.join('&');
}
function http({url,data={},method='get',params={}}){
    if(!url)return;
    
    if(sessionStorage.getItem('token')){
        headers['token']=sessionStorage.getItem('token');
    }
    
    if(method.toLowerCase()=='get'){
        return fetch(baseURL+url+'?'+json2url(params)).then((res)=>res.json());
    }else{
        return fetch(baseURL+url+'?'+json2url(params),{method,headers,body:JSON.stringify(data)}).then(res=>res.json());
    }
}

export default http